import { Controller } from "../../mvc/core/Controller";
import { IDragonTargetModule } from "../../../modules/interfaces/IDragonTargetModule";
import { IView } from "../../mvc/interfaces/IView";
import { TargetHintBtn } from "../view/mainUI/TargetHintBtn";
import { DragonTargetModule } from "../../../modules/DragonTargetModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { ServerCommand } from "../mvc/ServerCommand";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { ETargetStateType } from "../../../Message/Game/ETargetStateType";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SComprehensionTargetStateNew } from "../../../Message/Game/SComprehensionTargetStateNew";
import { SComprehensionCanEnd } from "../../../Message/Game/SComprehensionCanEnd";
import { EffectManager } from "../manager/EffectManager";
type int = number;
//class DragonTargetController
    
    export  class DragonTargetController extends Controller
    {
       

        public get dragonTargetModule(): IDragonTargetModule
        {
            if (null == this._dragonTargetModule) 
            {
                this.initView();
            }
            return this._dragonTargetModule;
        }

        public /* override */ get view(): IView
        {
            return this.initView();
        }

        public get targetHintBtn(): TargetHintBtn
        {
            if (this._target == null) 
            {
                this._target = new TargetHintBtn();
            }
            return this._target;
        }

        protected /* override */ initView(): IView
        {
            if (null == this._dragonTargetModule) 
            {
                this._dragonTargetModule = new DragonTargetModule();
            }
            return this._dragonTargetModule;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.TargetGetCondition, this.onGetConditionHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateComprehensionTargetNew, this.onReciveNewTargetStateHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicComprehensionCanEnd, this.onPublicComprehensionCanEndHandler);
            NetDispatcher.addCmdListener(ServerCommand.GetDragonTargetRewardSuccess, this.onGetDragonTargetRewardSuccess);
            NetDispatcher.addCmdListener(ServerCommand.GetDragonTargetChapterRewardSuccess, this.onGetDragonTargetChapterRewardSuccess);
            Dispatcher.addEventListener(EventName.DragonTargetNewChapterSelectChange, this.onDragonTargetNewChapterSelectChange);
            Dispatcher.addEventListener(EventName.DragonTargetNewDaySelectChange, this.onDragonTargetNewDaySelectChange);
            Dispatcher.addEventListener(EventName.GoGetDragonTargetReward, this.onGoGetDragonTargetReward);
            Dispatcher.addEventListener(EventName.GoGetDragonTargetChapterReward, this.onGoGteDragonTargetChapterReward);
            Dispatcher.addEventListener(EventName.OpenDragonTargetModule, this.onOpenDragonTargetModule);
            RolePlayer.instance.addEventListener(PlayerEvent.RoleLevelUpdate, this.onRoleLevelUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.TotalLoadRewardComment, this.onTotalLoadRewardComment);
            Dispatcher.addEventListener("DargonTargetTestTest", this.onTestTest);
            return;
        }

        /* internal  */onTestTest(arg1: DataEvent): void
        {
            return;
        }

        protected onRoleLevelUpdateHandler(arg1: Object): void
        {
            if (this._dragonTargetModule && !this._dragonTargetModule.isHide) 
            {
                this.dragonTargetModule.updateChapterInfo(this.getImgListInfo());
            }
            if (cache.dragonTarget.isHaveCanCompleteDay) 
            {
                this.showTargetEffect(true);
            }
            return;
        }

        /* internal  */onTotalLoadRewardComment(arg1: Object): void
        {
            if (this._dragonTargetModule && !this._dragonTargetModule.isHide) 
            {
                this.dragonTargetModule.updateChapterInfo(this.getImgListInfo());
            }
            if (cache.dragonTarget.isHaveCanCompleteDay) 
            {
                this.showTargetEffect(true);
            }
            return;
        }

        /* internal  */onOpenDragonTargetModule(arg1: DataEvent): void
        {
            if (this.dragonTargetModule.isHide) 
            {
                this.dragonTargetModule.show();
                this.dragonTargetModule.updateChapterInfo(this.getImgListInfo());
            }
            else 
            {
                this.dragonTargetModule.hide();
            }
            return;
        }

        /* internal  */getImgListInfo(): Array<any>
        {
            var loc2=null;
            var loc1=[];
            var loc3=1;
            while (loc3 < 11) 
            {
                loc2 = new Object();
                loc2.loginDay = cache.totalLoadReward.sattribute.value;
                loc2.curLevel = cache.role.entityInfo.level;
                loc2.index = loc3;
                loc1.push(loc2);
                ++loc3;
            }
            this.setMaxOpenChapter();
            return loc1;
        }

        /* internal  */setMaxOpenChapter(arg1: int=0): void
        {
            var loc2=0;
            var loc1=cache.role.entityInfo.level;
            if (arg1 != 0) 
            {
                loc2 = arg1;
            }
            else 
            {
                loc2 = cache.totalLoadReward.sattribute.value;
            }
            if (loc2 < 8) 
            {
                cache.dragonTarget.maxOpenChap = loc2;
            }
            else if (loc1 >= 50 && loc1 <= 59) 
            {
                cache.dragonTarget.maxOpenChap = 8;
            }
            else if (loc1 >= 60 && loc1 <= 69) 
            {
                cache.dragonTarget.maxOpenChap = 9;
            }
            else 
            {
                if (loc1 < 50) 
                {
                    cache.dragonTarget.maxOpenChap = 7;
                }
                if (loc1 >= 70) 
                {
                    cache.dragonTarget.maxOpenChap = 10;
                }
            }
            return;
        }

        /* internal  */onDragonTargetNewChapterSelectChange(arg1: DataEvent): void
        {
            if (this._dragonTargetModule != null) 
            {
                this.dragonTargetModule.chapterInfoChange(arg1.data as int);
                cache.dragonTarget.selectedChap = arg1.data as int;
            }
            return;
        }

        /* internal  */onDragonTargetNewDaySelectChange(arg1: DataEvent): void
        {
            if (this._dragonTargetModule != null) 
            {
                this.dragonTargetModule.dayRewardInfoChange(arg1.data as int);
            }
            return;
        }

        /* internal  */onGetConditionHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.target.getTargetCondition(loc1);
            return;
        }

        /* internal  */onGoGetDragonTargetReward(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.target.gainTargetAward(loc1);
            return;
        }

        /* internal  */onGoGteDragonTargetChapterReward(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.target.getChapterReward(loc1);
            return;
        }

        /* internal  */onGetDragonTargetRewardSuccess(arg1: Object): void
        {
            var loc1=arg1 as int;
            cache.dragonTarget.setStateByDay(loc1, ETargetStateType._ETargetStateTypeCompleted);
            cache.dragonTarget.setCanCompleteChap();
            MsgManager.showRollTipsMsg(Language.getString(35701));
            this.dragonTargetModule.updateChapterInfo(this.getImgListInfo());
            if (!cache.dragonTarget.isHaveCanCompleteDay) 
            {
                cache.dragonTarget.canCompleteChap = -1;
                this.showTargetEffect(false);
            }
            if (this._target && !this._target.isHide && cache.dragonTarget.isAllHadCompleted) 
            {
                this._target.hide();
            }
            return;
        }

        /* internal  */onGetDragonTargetChapterRewardSuccess(arg1: Object): void
        {
            var loc1=arg1 as int;
            var loc2=loc1 + 1000;
            cache.dragonTarget.setStateByDay(loc2, ETargetStateType._ETargetStateTypeCompleted);
            cache.dragonTarget.setCanCompleteChap();
            MsgManager.showRollTipsMsg(Language.getString(35701));
            this.dragonTargetModule.updateChapterInfo(this.getImgListInfo());
            if (!cache.dragonTarget.isHaveCanCompleteDay) 
            {
                cache.dragonTarget.canCompleteChap = -1;
                this.showTargetEffect(false);
            }
            if (this._target && !this._target.isHide && cache.dragonTarget.isAllHadCompleted) 
            {
                this._target.hide();
            }
            return;
        }

        /* internal  */onReciveNewTargetStateHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SComprehensionTargetStateNew;
            var loc2=loc1.stateDict;
            cache.dragonTarget.stateDic = loc2;
            this.setMaxOpenChapter(loc1.value);
            cache.dragonTarget.setCanCompleteChap();
            this.showTargetBtn();
            if (cache.dragonTarget.isHaveCanCompleteDay) 
            {
                this.showTargetEffect(true);
            }
            return;
        }

        /* internal  */onPublicComprehensionCanEndHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SComprehensionCanEnd;
            if (loc1.canComplete) 
            {
                if (loc1.targetDay != 0) 
                {
                    cache.dragonTarget.setStateByDay(loc1.targetDay, ETargetStateType._ETargetStateTypeCanComplete);
                    if (loc1.chapter <= cache.dragonTarget.maxOpenChap) 
                    {
                        cache.dragonTarget.canCompleteChap = loc1.chapter;
                        cache.dragonTarget.canCompleteDay = loc1.targetDay;
                    }
                }
                else 
                {
                    cache.dragonTarget.setStateByDay(loc1.chapter, ETargetStateType._ETargetStateTypeCanComplete);
                    if (loc1.chapter - 1000 <= cache.dragonTarget.maxOpenChap) 
                    {
                        cache.dragonTarget.canCompleteChap = loc1.chapter - 1000;
                        cache.dragonTarget.canCompleteDay = 0;
                    }
                }
                if (loc1.chapter == cache.dragonTarget.selectedChap) 
                {
                    this.dragonTargetModule.chapterInfoChange(loc1.chapter);
                }
                this.showTargetEffect(true);
            }
            return;
        }

        /* internal  */showTargetBtn(): void
        {
            if (!cache.dragonTarget.isAllHadCompleted) 
            {
                this.targetHintBtn.show();
            }
            return;
        }

        public showTargetEffect(arg1: Boolean): void
        {
            var loc1=null;
            var loc2=null;
            if (!this._target) 
            {
                return;
            }
            if (arg1) 
            {
                loc1 = new Object /* flash.filters.GlowFilter */(268435302, 1, 6, 6, 4);
                loc2 = [loc1];
                EffectManager.glowFilterReg(this._target, loc2, 0.8, 10, 5, 0);
            }
            else 
            {
                EffectManager.glowFilterUnReg(this._target);
            }
            return;
        }

        private /* var */_dragonTargetModule: IDragonTargetModule;

        private /* var */_target: TargetHintBtn;
    }
